package base.thread;

import org.junit.Test;

import java.util.concurrent.*;

public class ThreadPoolMain {

    class Task implements Runnable{
        int name;
        public Task(int name){this.name=name;}
        @Override
        public void run() {
            System.out.println("Task " + name + " is deal with " + Thread.currentThread().getName());
            try {
                // need execute time
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    @Test
    public void maxAndQueueNotEnough(){
        int taskSize = 20;
        int corePoolSize = 5;
        int maximumSize = 10;
        long keepAliveTime = 0L;
        TimeUnit timeUnit = TimeUnit.SECONDS;
        int queueSize = 5;
        ThreadPoolExecutor executor  =
                new ThreadPoolExecutor(corePoolSize, maximumSize, keepAliveTime, timeUnit, new LinkedBlockingQueue<>(queueSize));
        for(int i = 1; i <= taskSize; i++){
            executor.execute(new Task(i));
        }
        executor.shutdown();
    }

    @Test
    public void maxAndQueueEnough(){
        int corePoolSize = 5;
        int maximumSize = 10;
        long keepAliveTime = 0L;
        TimeUnit timeUnit = TimeUnit.SECONDS;
        int queueSize = 10;
        ThreadPoolExecutor executor  =
                new ThreadPoolExecutor(corePoolSize, maximumSize, keepAliveTime, timeUnit, new LinkedBlockingQueue<>(queueSize));
        for(int i = 1; i <= 20; i++){
            executor.execute(new Task(i));
        }
        executor.shutdown();
    }

    /**
     *  个体户 自己一个人儿干
     * @author shenqihan
     * @date 2022/3/18 10:48
     */
    @Test
    public void runSingleThreadPool(){
        int taskSize = 10;
        ExecutorService pool = Executors.newSingleThreadExecutor();
        for(int i = 1; i <= taskSize; i++){
            pool.execute(new Task(i));
        }
        pool.shutdown();
    }

    /**
     *  纯外包公司 没有正式员工
     * @author shenqihan
     * @date 2022/3/18 10:50
     */
    @Test
    public void runCachedThreadPool(){
        int taskSize = 10;
        ExecutorService pool = Executors.newCachedThreadPool();
        for(int i = 1; i <= taskSize; i++){
            pool.execute(new Task(i));
        }
        pool.shutdown();
    }

    /**
     * 全是正式员工 正规国企
     * 来活就招人
     * @author shenqihan
     * @date 2022/3/18 10:54
     *
     */
    @Test
    public void runFixedThreadPool(){
        int coreSize = 5;
        int taskSize = 10;
        ExecutorService pool = Executors.newFixedThreadPool(coreSize);
        for(int i = 1; i <= taskSize; i++){
            pool.execute(new Task(i));
        }
        pool.shutdown();
    }

    public static void main(String[] args) {
        // 生产者 - 消费者模型
        ThreadPoolMain main = new ThreadPoolMain();
        main.maxAndQueueNotEnough();
        main.maxAndQueueEnough();
        main.runSingleThreadPool();
        main.runCachedThreadPool();
        main.runFixedThreadPool();
    }

}
